package com.yangyang.rpc.admin.controller;

import com.yangyang.rpc.admin.bean.RpcInfo;
import com.yangyang.rpc.core.registry.zookeeper.ZkServiceDiscovery;
import org.apache.commons.collections4.MapUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created by chenshunyang on 2016/11/28.
 */
@RestController
@RequestMapping("/rpc")
public class RpcController {


    @RequestMapping("/pageList")
    @ResponseBody
    public Map<String, Object> pageList(Model model, String iface){
        model.addAttribute("iface", iface);

        ZkServiceDiscovery.watchNode();
        List<RpcInfo> list = new ArrayList<RpcInfo>();
        if (MapUtils.isNotEmpty(ZkServiceDiscovery.serviceAddress)) {
            for (Map.Entry<String, Set<String>> item: ZkServiceDiscovery.serviceAddress.entrySet()) {
                RpcInfo info = new RpcInfo();
                info.setIface(item.getKey());
                if (item.getValue() != null) {
                    info.setProviders(new ArrayList<String>(item.getValue()));
                }
                list.add(info);
            }
        }
        // package result
        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("recordsTotal", list.size());		// 总记录数
        maps.put("recordsFiltered", list.size());	// 过滤后的总记录数
        maps.put("data", list);
        return maps;
    }
}
